﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;
using System.Globalization;

public partial class UserControl_ucStaffList : System.Web.UI.UserControl
{
    TblCustomersDAO CustomersDAO = new TblCustomersDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadStaff();
            LoadDetailStaff();
        }
    }

    private void LoadDetailStaff()
    {
        string ID = Request.QueryString["id"];
        if (ID == null)
        {

        }
        else
        {
            TblCustomers customer = CustomersDAO.CustomerById(Global.ConvertToInt(ID));
            txtCompany.Text = customer.Companyname;
            txtEmail.Text = customer.Emailaddress;
            txtFax.Text = customer.Phonefacsimile;
            txtFName.Text = customer.CustomerFirstname;
            txtHomePhone.Text = customer.Phonehome;
            txtLName.Text = customer.CustomerLastname;
            txtMobile.Text = customer.Phonemobile;
            txtNumber.Text = customer.Postalnumber;
            txtPostcode.Text = customer.Postalpostcode;
            txtStreet.Text = customer.Postalstreet;
            txtWorkPhone.Text = customer.Phonework;
            drTitle.SelectedValue = customer.CustomerTitle;


            if (customer.Postalsuburb == "")
            {
                drState.SelectedValue = "";
            }
            else
            {

                drState.SelectedValue = customer.Postalstate;
            }


            if (customer.Postalsuburb == "")
            {
                drSuburb.SelectedValue = "";
            }
            else
            {
                LoadSuburb();
                drSuburb.Text = customer.Postalsuburb;
            }

        }
    }

    protected void btnsavenew_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            TblCustomers customer = new TblCustomers();

            customer.CustomerTitle = drTitle.SelectedValue;
            customer.CustomerFirstname = txtFName.Text;
            customer.CustomerLastname = txtLName.Text;
            customer.CustomerFullname = drTitle.SelectedValue.ToString() + " " + txtFName.Text + " " + txtLName.Text;
            customer.Postalnumber = txtNumber.Text;
            customer.Companyname = txtCompany.Text;
            customer.Postalstreet = txtStreet.Text;
            // customer.Postalsuburb = drSuburb.SelectedItem.Text;
            customer.Postalpostcode = txtPostcode.Text;
            customer.Postalstate = drState.SelectedValue;
            customer.IsAdmin = Convert.ToBoolean("True");
            if (drSuburb.SelectedValue == "")
            {

            }
            else
            {
                customer.Postalsuburb = drSuburb.SelectedItem.Text;
            }
            customer.Phonework = txtWorkPhone.Text;
            customer.Phonehome = txtHomePhone.Text;
            customer.Phonemobile = txtMobile.Text;
            customer.Phonefacsimile = txtFax.Text;
            customer.Emailaddress = txtEmail.Text;
            //customer.CustomerUsername = txtUsername.Text;
            customer.Emailpassword = Encryption.EncryptToString(txtPass.Text);
            customer.PreferContactMethod = 0;
            customer.DateCreated = DateTime.Now;

            DataTable DT_Customer = null;
            if (Session[Global.SESSION_LOGGED_USER] == null)
            {
                customer.CustomerIndex = 0;
                DT_Customer = CustomersDAO.InsUpdCustomers(customer, 1);
                
            }
            else
            {
                customer.CustomerIndex = ((TblCustomers)Session[Global.SESSION_LOGGED_USER]).CustomerIndex;
                CustomersDAO.InsUpdCustomers(customer, 2);
            }

            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (flag)
        {
            Label3.Text = "successful";
            //message_green.Style.Value = "inline";
            Response.Redirect("Default.aspx");
        }

    }
    private void LoadSuburb()
    {
        drSuburb.DataSource = CustomersDAO.GetSuburb(drState.SelectedValue, txtPostcode.Text);
        drSuburb.DataTextField = "suburb";
        drSuburb.DataValueField = "suburb";
        drSuburb.DataBind();
    }

    private void LoadStaff()
    {
        try
        {
            grdStaffList.DataSource = CustomersDAO.GetStaffList(1);
            grdStaffList.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }

    }

    protected void txtPostcode_TextChanged(object sender, EventArgs e)
    {
        LoadSuburb();
    }
    protected void grdStaffList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdStaffList.PageIndex = e.NewPageIndex;
        LoadStaff();
    }

    //protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    //{
    //   // string id = Request.QueryString["id"];
    //    bool flag = false;
    //    try
    //    {
           
    //        TblCustomers customer = new TblCustomers();           
    //        customer.CustomerIndex = Convert.ToInt32(((ImageButton)sender).CommandArgument);
    //        int idDelete = Convert.ToInt32(((ImageButton)sender).CommandArgument);
    //        customer = CustomersDAO.CustomerById(idDelete);
    //        CustomersDAO.DeleteStaff(customer);

           
    //        flag = true;
    //    }
    //    catch (Exception ex)
    //    {
    //        Session[Global.SESSION_ERROR] = ex;
    //        Response.Redirect(Global.ERROR_PAGE_ADMIN);
    //    }
    //    if (flag)
    //    {
    //        LoadStaff();
    //    }

    //}
    protected void btnReset_Click(object sender, EventArgs e)
    {
        Session[Global.SESSION_ADMIN_USER] = null;
        Response.Redirect("~/StaffList.aspx");
        txtCompany.Text = "";
        txtEmail.Text = "";
        txtFax.Text = "";
        txtFName.Text = "";
        txtHomePhone.Text = "";
        txtLName.Text = "";
        txtMobile.Text = "";
        txtNumber.Text = "";
        txtPostcode.Text = "";
        txtStreet.Text = "";
        txtWorkPhone.Text = "";
        drState.SelectedValue = "";
        drSuburb.SelectedItem.Text = "";
        drTitle.SelectedItem.Text = "";
    }
    protected void txtFName_TextChanged(object sender, EventArgs e)
    {
        txtFName.Text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(txtFName.Text);
    }
    protected void txtLName_TextChanged(object sender, EventArgs e)
    {
        txtLName.Text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(txtLName.Text);
    }
}
